//
//  STSpreadOrderListViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/12/24.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "STSpreadOrderListViewModel.h"

#import "SpreadOrderListCellModel.h"

#import "SpreadOrdersListData.h"

@interface STSpreadOrderListViewModel()

@property (nonatomic, readwrite, strong) RACCommand* requestDataCmd;

@property (nonatomic, readwrite, strong) RACCommand* selectCmd;

@end

@implementation STSpreadOrderListViewModel

- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    self.cellHeight = 100.0f;
    
    self.lastPage = 100;
    self.shouldMultiSections = NO;
    /// viewDidLoad后加载数据
    self.shouldRequestRemoteDataOnViewDidLoad = YES;
    /// 允许下拉刷新
    self.shouldPullDownToRefresh = YES;
    /// 允许上拉加载
    self.shouldPullUpToLoadMore = YES;
    
    self.selectCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSIndexPath* indexPath)
    {
        @strongify(self);
        
        @weakify(self);
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
        {
            @strongify(self);
            
            SpreadOrderListCellModel* cm = self.dataSource[indexPath.row];
            SpreadOrderListData* curData = cm.data;
            
            [subscriber sendNext:curData.spreadID];
            [subscriber sendCompleted];
            
            return nil;
        }];
    }];
}

- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page
{
    @weakify(self)
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self);
        
        UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
        
        NSDictionary* postData = interface_param_spread_spreadList(um.userData.userID, @(page + 1), @(List_PageSize));
        
        @weakify(self);
        return [[AFNetWorkUtils racPOSTWthURL:url_interface_spread_spreadList params:postData] subscribeNext:^(id x)
                {
                    @strongify(self)
                    
                    /// 转化数据
                    @weakify(self);
                    
                    SpreadOrdersListData* data = [SpreadOrdersListData modelWithJSON:x];
                    
                    NSArray* datas = data.datas;
                    
                    NSArray *viewModels = [datas.rac_sequence map:^id(id data)
                                           {
                                               @strongify(self);
                                               
                                               SpreadOrderListCellModel *itemViewModel = [[SpreadOrderListCellModel alloc] initWithData:data cellHeight:self.cellHeight];
                                               
                                               return itemViewModel;
                                           }].array;
                    
                    if (viewModels == nil || viewModels.count < List_PageSize)
                    {
                        self.lastPage = self.page;
                        
                        if (viewModels == nil)
                            viewModels = @[];
                    }
                    else
                    {
                        self.lastPage = 999;
                    }
                    
                    if (self.shouldPullUpToLoadMore)
                    {
                        if (0 == page)
                        {
                            self.dataSource = viewModels;
                        }
                        else
                        {
                            if (self.dataSource == nil)
                            {
                                self.dataSource = viewModels;
                            }
                            else
                            {
                                self.dataSource = [self.dataSource arrayByAddingObjectsFromArray:viewModels];
                            }
                        }
                    }
                    else
                    {
                        self.dataSource = viewModels;
                    }
                    
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                }   error:^(NSError *error) {
                    //错误处理
                    
                    [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:error.userInfo[customErrorInfoKey]}]];
                    [subscriber sendCompleted];
                }
                ];
        
        return nil;
    }];
}

@end
